/*******************************************************************************************
 * Copyright (c) 2006-2009 Alex Robson, Rob Simmons, Craig Israel, and Rachel Twyford
 *
 * Use of this software in any form requires that you have both 
 * read and agreed to the following terms:
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions: 
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution.
 * Neither the name of Nvigorate nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Nvigorate.Logging;
using System.Linq;

namespace Nvigorate.Common.Reflection
{
	public sealed partial class Reflector
	{
	    #region Public Attribute Static Methods

        public static IEnumerable<FieldInfo> GetFieldInfoByAttribute<T>(Type type) where T : Attribute
        {
            return GetFieldInfoList(type)
                .Where(f => f.GetCustomAttributes(typeof(T), recursive).Length != 0);
        }

        public static IEnumerable<T> GetFieldAttributes<T>(Type type, string fieldName) where T : Attribute
        {
            return GetFieldInfo(type, fieldName, SearchHierarchy).GetCustomAttributes(typeof (T), SearchHierarchy).Cast<T>();
        }

        public static IEnumerable<Tuple<string, List<T>>> GetPropertyAttributes<T>(Type type) where T : Attribute
        {
            return GetPropertyInfoList(type)
                .Select(p => Tuple.Create(p.Name,
                                          p.GetCustomAttributes(typeof (T), SearchHierarchy).Cast<T>().ToList()));
        }

        public static IEnumerable<T> GetPropertyAttributes<T>(Type type, string propertyName) where T : Attribute
        {
            return GetPropertyInfo(type, propertyName, SearchHierarchy).GetCustomAttributes(typeof (T), SearchHierarchy).Cast<T>();
        }

        public static IEnumerable<PropertyInfo> GetPropertyInfoByAttribute<T>(Type type) where T : Attribute
        {
            return GetPropertyInfoList(type)
                .Where(p => p.GetCustomAttributes(typeof (T), recursive).Length != 0);
        }
	    
        private static IEnumerable<MemberInfo> GetMemberInfoByAttribute<T>(Type type, MemberTypes memberType) where T : Attribute
        {
            return GetMemberInfo(type, MemberTypes.All, recursive)
                .Where(m =>
                       m.GetCustomAttributes(true).ToList().Exists(a => a.GetType().Equals(typeof (T))));
        }
		
		/// <summary>
		/// Searches the target class type for attributes of the specified type
		/// and returns them in an ArrayList.
		/// </summary>
		/// <param name="type">The class type to search for attributes</param>
		/// <typeparam name="T">The type of attributes to search the class for</typeparam>typeparamref>
		/// <returns>List containing all attributes of the specified type
		/// applied to the target type</returns>
		public	static		List<T> GetClassAttributes<T>(Type type) where T:Attribute
		{
			return GetGenericListFromArray<T>(type.GetCustomAttributes(typeof(T), recursive));
		}
		#endregion

	    public static ConstructorInfo GetConstructor(IEnumerable<object> arguments, Type type)
	    {
	        var equivalency = new TypeEquivalenceComparer();
	        var argumentTypes = arguments.Select(a => a.GetType());
	        return type.GetConstructors()
	            .Where(c => c.GetParameters().Length == arguments.Count())
	            .FirstOrDefault(c => c.GetParameters().Select(p => p.ParameterType).SequenceEqual(argumentTypes, equivalency));
	    }
	}
}